﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Circa.Tools;
using Configuration = Circa.Configuration;

namespace Circa.Protocol.Irc {

    public class GenericSelfUserImpl: SelfUserImpl {

        #region Properties

        public override string Name { get; set; }
        public Server Server { get; set; }
        public Network Network {
            get {
                return Server.Network;
            }
        }
        
        #endregion Properties


        #region Private fields

        private AutoSafeObjectDictionary<string, List<Action<IrcMessage>>> verbs = new AutoSafeObjectDictionary<string, List<Action<IrcMessage>>>( );
        private SafeDictionary<string, ITargetable> targets = new SafeDictionary<string, ITargetable>( );

        private Configuration.Identity identity;

        #endregion Private fields


        #region Constructors
        
        public GenericSelfUserImpl( ): base( ) {
            //verbs.Add( "401", ErrNoSuchNick );
            //verbs.Add( "431", ErrNoNickNameGiven );
            //verbs.Add( "432", ErrErroneousNickName );
            verbs["433"].Add( ErrNickNameInUse );
            verbs["VerbNICK"].Add( VerbNICK );
        }

        public GenericSelfUserImpl( Server _parent, Configuration.Identity _identity ) {
            Server = _parent;
            identity = _identity;
        }

        #endregion


        //
        // Implementation
        //

        protected override void ErrNickNameInUse( IrcMessage m ) {
            string newName = null;

            // Does the in-use nick exist in the list of configured nicks?
            int currentNameIndex = identity.Nicks.IndexOf( m.Args[ 0 ] );
            if ( currentNameIndex > -1 ) {
                currentNameIndex = ( currentNameIndex + 1 ) % identity.Nicks.Count;
                newName = identity.Nicks.ElementAt( currentNameIndex );
            }

            if ( null == newName ) {
                newName = Name;
                if ( newName.Length < Server.ProtocolProperties.NickNameLength ) {
                    // append underscores until we run out of room...
                    newName += "_";
                } else {
                    // then start rotating the nick
                    newName = newName.Substring( 0, Server.ProtocolProperties.NickNameLength );
                    newName = newName.Substring( 1 ) + newName.Substring( 0, 1 );
                }
            }

            Server.Perform.Nick( newName );
        }

        protected override void VerbNICK( IrcMessage m ) {
            Debug.Print( "SelfEntity.VerbNICK: renaming from {0} to {1} (origin={2} target={3})", Name, m.Args[ 0 ], m.OriginName, m.TargetName );

            string oldName = Name;
            Name = m.Args[ 0 ];
            Server.RenameTarget( oldName, Name );
        }

    }

}
